﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRVV05LIST(DataSet db)
        {
            int x = 4, y = 1;
            string VKPPOSTYP = "";
            DataTable dtOutput = new DataTable();
            try
            {
                /*
                  <DataGridTextColumn Binding="{Binding Path=VKPLIEFTERM}" Header="预计发货日期"  Width="Auto" MinWidth="40"/>
                    <DataGridTextColumn Binding="{Binding Path=VKPPOSTYP}" Header="类型"   Width="Auto"  MinWidth="40"/>
                    <DataGridTextColumn Binding="{Binding Path=KDKUNDNA}" Header="客户"   Width="Auto"  MinWidth="40"/>
                    <DataGridTextColumn Binding="{Binding Path=KDSTRASSE1}" Header="送货地点"   Width="Auto"  MinWidth="40"/>                    
                    <DataGridTextColumn Binding="{Binding Path=VKPAUFKRS}"   Header=""  Width="Auto"  MinWidth="40"/>
                    <DataGridTextColumn Binding="{Binding Path=VKPAUFLFD}" Header="PPO"   Width="Auto"  MinWidth="40"/>
                    <DataGridTextColumn Binding="{Binding Path=VKKBESTNR}" Header="合同号"  Width="Auto" MinWidth="40"/>
                    <DataGridTextColumn Binding="{Binding Path=VKKMENGE1}" Header="重量"   Width="Auto"  MinWidth="40"/>                    
                    <DataGridTextColumn Binding="{Binding Path=VKPART}" Header="品种"   Width="Auto"  MinWidth="40"/>
                    <DataGridTextColumn Binding="{Binding Path=VKPFARBE}" Header="色号"   Width="Auto"  MinWidth="40"/> 
                    <DataGridTextColumn Binding="{Binding Path=VKPMENGE1}" Header="数量" Width="Auto" MinWidth="40"/>
                    <DataGridTextColumn Binding="{Binding Path=QTYPLAN}" Header="计划量" Width="Auto" MinWidth="40"/>
                    <DataGridTextColumn Binding="{Binding Path=VKPZUGET}" Header="产出量" Width="Auto" MinWidth="40"/>       
                    <DataGridTextColumn Binding="{Binding Path=VKPMEEH}" Header="单位"  Width="Auto" MinWidth="40"/> 
                    <DataGridTextColumn Binding="{Binding Path=VKPPREIS}" Header="价格"  Width="Auto" MinWidth="40"/>
                    <DataGridTextColumn Binding="{Binding Path=PUM}" Header="UM"  Width="Auto" MinWidth="40"/>                    
                </DataGrid.Columns>*/
                #region======Title===========
                exAgent.SetCellValue(3, 1, "预计发货日期"); dtOutput.Columns.Add("VKPLIEFTERM", typeof(System.String));
                exAgent.SetCellValue(3, 2, "客户"); dtOutput.Columns.Add("KDKUNDNA", typeof(System.String));
                exAgent.SetCellValue(3, 3, "送货地点"); dtOutput.Columns.Add("KDSTRASSE1", typeof(System.String));
                exAgent.SetCellValue(3, 4, "预计重量kg"); dtOutput.Columns.Add("VKKMENGE1", typeof(System.Decimal));
                exAgent.SetCellValue(3, 5, "类型"); dtOutput.Columns.Add("VKPPOSTYP", typeof(System.String));
                exAgent.SetCellValue(3, 6, " "); dtOutput.Columns.Add("VKPAUFKRS", typeof(System.String));
                exAgent.SetCellValue(3, 7, "PPO"); dtOutput.Columns.Add("VKPAUFLFD", typeof(System.String));
                exAgent.SetCellValue(3, 8, "品种"); dtOutput.Columns.Add("VKPART", typeof(System.String));
                exAgent.SetCellValue(3, 9, "色号"); dtOutput.Columns.Add("VKPFARBE", typeof(System.String));
                exAgent.SetCellValue(3, 10, "价格"); dtOutput.Columns.Add("VKPPREISX", typeof(System.Decimal));
                exAgent.SetCellValue(3, 11, "PUM"); dtOutput.Columns.Add("PUM", typeof(System.String));
                exAgent.SetCellValue(3, 12, "预计重量kg"); dtOutput.Columns.Add("VKPMENGE1", typeof(System.String));
                exAgent.SetCellValue(3, 13, "已发货重量kg"); dtOutput.Columns.Add("VKPFREIGE", typeof(System.String));
                #endregion

                #region======ConvertData===
                #endregion
                var qGW = from GW in db.Tables["VKPOS"].AsEnumerable()
                          group GW by new
                          {
                              PT = GW["VKPPOSTYP"].ToString(),
                          }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                              };
                foreach (var GW in qGW)
                {
                    VKPPOSTYP = GW.KEY.PT;
                    exAgent.RenameSheet(date);
                    DataRow[] rows = db.Tables["VKPOS"].Select();
                    DataTable dtSoruce = rows.CopyToDataTable();
                    dtOutput.Clear();
                    dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);
                    foreach (DataRow rx in dtOutput.Rows)
                    {
                    } 
                    #region====Excel========== 
                    int count = dtOutput.Rows.Count;
                    exAgent.RangeCopy(4, 50, 5, 4 + count);
                    exAgent.DataTableToExcel(dtOutput, x, 1); 
                    MergeSame(x,count);
                    exAgent.SetCellValue(1, 1, 1);
                    exAgent.BatchConvert2Number(4, 4, count + 3, 4);
                    exAgent.BatchConvert2Number(4, 7, count + 3, 7);
                    exAgent.BatchConvert2Number(4, 10, count + 3, 10);
                    exAgent.BatchConvert2Number(4, 12, count + 3, 13);
                    exAgent.SetCellValue(1, 1, " ");
                    #endregion
                    break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }
        }
        public void MergeSame(int xStart,int rows)
        {
            int x1, y1, x2, y2;
            string str = "";
            string str1;
            y1 = 5;
            y2 = 5;
            x1 = 4;
            x2 = 4;

            for (int i = xStart; i <= rows + xStart; i++)
            {
                str1 = (string)exAgent.GetCellValue(i, 1) + (string)exAgent.GetCellValue(i, 2);

                if (str == str1)
                {
                    x2 = i;
                }
                else
                {
                    if (x2 > x1)
                    {
                        exAgent.Merge(x1, 1, x2, 1);
                        exAgent.Merge(x1, 2, x2, 2);
                        exAgent.Merge(x1, 3, x2, 3);
                        exAgent.Merge(x1, 4, x2, 4);
                    }
                    x1 = i;
                    str = str1;
                }
            }
        }
    }
}